package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.SysUser;
import com.itheima.service.UserService;
import com.itheima.utils.PasswordEncoderUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

	@Reference
	private UserService userService;

	@Autowired
    PasswordEncoderUtils passwordEncoderUtils;

	@RequestMapping("/getUsername")
	public Result getUsername(){
		log.debug("获取用户名!!");
		//获取安全框架上下文对象
		SecurityContext context = SecurityContextHolder.getContext();
		//获取认证对象
		Authentication authentication = context.getAuthentication();
		//获取 Principal 重要对象
		Object principal = authentication.getPrincipal();
		if(principal instanceof User){
			User user = (User) principal;
			String username = user.getUsername();
			return new Result(true, MessageConst.GET_USERNAME_SUCCESS, username);
		}
		return new Result(false,MessageConst.GET_USERNAME_FAIL);
	}

	@RequestMapping("/loginSuccess")
	public ModelAndView loginSuccess(){
		return new ModelAndView("redirect:http://oms.web.health:83/pages/main.html");
	}

	@RequestMapping("/loginError")
	public ModelAndView doError(){
		return new ModelAndView("redirect:http://oms.web.health:83/login.html");
	}

	/**
	 * 添加使用用户信息
	 * @param sysUser
	 * @param roleIds
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody SysUser sysUser,Integer[] roleIds){
		//创建加密工具类
		//PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
		log.debug("add---->sysUser:"+sysUser+"add---->roleIds:"+ Arrays.toString(roleIds));
		//将添加用户是的密码进行加密添加到数据库
		/*
		Map<String, PasswordEncoder> encoders = new HashMap();
		encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));
		DelegatingPasswordEncoder MD5 = new DelegatingPasswordEncoder("MD5",encoders);
		*/
		sysUser.setPassword(passwordEncoderUtils.encode(sysUser.getPassword()));
		userService.add(sysUser,roleIds);
		log.debug("添加用户成功");
		return new Result(true,"添加用户成功");
	}

	/**
	 * 查询所用用户分页显示在用户管理页面
	 * 传入参数为QueryPageBean对象
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
		log.debug("UserController:findPage----->queryPageBean参数:"+queryPageBean);
		PageResult pageResult = userService.findPage(queryPageBean);
		log.debug("获取的分页参数pageResult:"+pageResult.toString());
		return pageResult;
	}

	/**
	 * 通过点击用户的id查询用户信息回显
	 */
	@RequestMapping("/findUserById")
	public Result findUserById(Integer id){
		log.debug("findRoleById方法的参数:id==="+id);
		Map<String,Object> map = userService.findUserById(id);
		log.debug("返回用户数据成功map"+map);
		return new Result(true,"返回用户数据成功",map);
	}

	/**
	 * 通过当前用户id查询所对应的的所有角色id
	 * @param userId
	 * @return
	 */
	@RequestMapping("/findroleIdsByUserId")
	public Result findroleIdsByUserId(Integer userId){
		log.debug("findroleIdsByUserId---->参数:"+userId);
		List<Integer> roleIds = userService.findroleIdsByUserId(userId);
		log.debug("获取的角色id数组为:"+roleIds);
		return new Result(true,"获取角色id数组成功",roleIds);
	}

	/**
	 * 编辑用户信息
	 * @param sysUser
	 * @param roleIds
	 * @return
	 */
	@RequestMapping("/edit")
	public Result edit(@RequestBody SysUser sysUser,Integer[] roleIds){
		log.debug("edit方法--->参数sysUser:"+sysUser.toString());
		log.debug("edit方法--->参数roleIds:"+Arrays.toString(roleIds));
		userService.edit(sysUser,roleIds);
		return new Result(true,"修改用户信息成功");
	}

	@RequestMapping("/delUserById")
	public Result delUserById(Integer id){
		log.debug("delUserById方法参数id:"+id);
		userService.delUserById(id);
		return new Result(true,"删除用户成功");
	}

//	@RequestMapping("/login")
//	public Result login(String username, String password){
//		log.debug("oms backend,user:"+username+" ,password:"+password);
//		if(userService.login(username,password)){
//			log.debug("login ok!!!");
//			return new Result(true, MessageConst.ACTION_SUCCESS);
//		}else{
//			log.debug("login fail");
//			return new Result(false,MessageConst.ACTION_FAIL);
//		}
//	}
}